
/*
* @version: 1.0
* @Author: zhanggq
* @Date: 2023-02-06 9:01:00
* @LastEditTime: 2023-02-8 16:39:30
* @Description: 映射关系管理
*/

#ifndef   __IEC104_SERVIVE_MANAGER__
#define   __IEC104_SERVIVE_MANAGER__

#include "DataCenterManager.h"
#include "MappingRelationManager.h"
#include "../../include/lib60870/iec60870_common.h"
#include <list>
using namespace  std;


//遥信数据
typedef struct __iec104_ptObj_{
    int ioa;    //信息对象地址
    int val;    //信息对象的值
    CP56Time2a acq_time;
    __iec104_ptObj_(){
        ioa = 0;
        val = 0;
        acq_time = nullptr;
    }
}ST_IEC104_PTVAL;
//遥测数据
typedef struct  __iec104_mearObj_
{
    int ioa;
    float fval;
    CP56Time2a acq_time;
    __iec104_mearObj_(){
        ioa = 0;
        fval = 0.0f;
        acq_time = nullptr;
    }
}ST_IEC104_MEAR_VAL;
//遥脉数据
typedef struct __iec10_InterTotalObj_
{
    int  ioa;
    BinaryCounterReading totalval;
    CP56Time2a  acq_time;
    __iec10_InterTotalObj_(){
        ioa = 0;
        totalval = new sBinaryCounterReading;
        memset(totalval->encodedValue, 0, 5);
        acq_time = nullptr;
    }
    //~__iec10_InterTotalObj_(){
    //    delete totalval;
    //}
}ST_IEC104_INTERTOTAL_VAL;

//遥信数据（公共地址、标识类型、传送原因和信息对象的地址与值）
typedef struct __apdu_point_data{
    int ti;
    int cot;
    std::list<ST_IEC104_PTVAL> lstObjs;
    __apdu_point_data(){
        ti = 0;
        cot = 0;
        lstObjs.clear();
    }
    ~__apdu_point_data(){
        for (auto itt = lstObjs.begin(); itt != lstObjs.end();itt++)
        {
            free(itt->acq_time);
        }
        lstObjs.clear();
    }
}ST_APDU_POINT_DATA;
//遥测数据（公共地址、标识类型、传送原因和信息对象的地址与值）
typedef struct   __apdu_measuredval_data
{
    int ti;
    int cot;
    std::list<ST_IEC104_MEAR_VAL> lstObjs;
    __apdu_measuredval_data(){
        ti = 0;
        cot = 0;
        lstObjs.clear();
    }
    ~__apdu_measuredval_data(){
        for (auto ito = lstObjs.begin(); ito != lstObjs.end();ito++)
        {
            free(ito->acq_time);
        }
        lstObjs.clear();
    }
}ST_APDU_MEASUREDVAL_DATA;
//遥脉数据
typedef  struct   __apdu_intertotal_data
{
    int ti;
    int cot;
    std::list<ST_IEC104_INTERTOTAL_VAL>  lstObjs;
    __apdu_intertotal_data(){
        ti = 0;
        cot = 0;
        lstObjs.clear();
    }
    ~__apdu_intertotal_data(){
        for (auto ito = lstObjs.begin(); ito != lstObjs.end(); ito++)
        {
            free(ito->acq_time);
        }
        lstObjs.clear();
    }
}ST_APDU_INTERRTOTAL_DATA;

class CIec104ServiceManager
{
public:
    CIec104ServiceManager();
    ~CIec104ServiceManager();

    void init(std::shared_ptr<CConfigManager> cm, std::shared_ptr<DataCenterManager> dcm);

    int  get_dc_realtime_data(int& ca, ST_APDU_POINT_DATA& stApduPtData, ST_APDU_MEASUREDVAL_DATA& stApduMearData, ST_APDU_INTERRTOTAL_DATA& stCounterIntertData);

    int  get_report_data(std::string& dev, std::list<ST_CHANGE_REPORT_DATAPT>& lstChangeReportPts, int& ca, ST_APDU_POINT_DATA& stApduPtData, 
        ST_APDU_MEASUREDVAL_DATA& stApduMearData, ST_APDU_INTERRTOTAL_DATA&  stCounterInterrData);

protected:
    void  iec104Obj_classify(std::string& val, std::string& timestamp, std::shared_ptr<ST_104_INFO_OBJECT>  st104Obj, 
                                ST_APDU_POINT_DATA& stApduPtData, ST_APDU_MEASUREDVAL_DATA& stApduMearData,
                                ST_APDU_INTERRTOTAL_DATA& stApduCounterInterrData);
    CP56Time2a get_cp56_timestamp(std::string& strtime);

    void  get_iec104_ca();

private:
    std::shared_ptr<CConfigManager>     c_cm;
    std::shared_ptr<DataCenterManager>  c_dcm;

    int     c_ca; 
};


#endif
